#include "pch.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

//
class Solution {
public:
	vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
		int len1 = nums1.size();
		int len2 = nums2.size();
		if (!len1 || !len2) return {};

		unordered_map<int, int> m;

		for (int num : nums1) {
			++m[num];
		}

		vector<int> res{};
		for (int num : nums2)
		{
			if (m.count(num))
			{
				res.push_back(num);
				--m[num];
				if (m[num] == 0)
				{
					m.erase(num);
				}
			}
		}
		return res;
	}
};

int main()
{
	Solution s;
	vector<int> arr1{ 1,2,2,1 };
	vector<int> arr2{ 2};

	vector<int> res = s.intersect(arr1,arr2);
	for (auto it = res.begin();it != res.end();it++)
	{
		cout << (*it) << " ";
	}
	return 0;
}